package com.example.xd.mvp_project.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.xd.mvp_project.R;
import com.example.xd.mvp_project.adapter.FancListAdapter;
import com.example.xd.mvp_project.base.BaseActivity;
import com.example.xd.mvp_project.model.bean.FancBean;
import com.example.xd.mvp_project.widget.SimpleSideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class FansListActivity extends BaseActivity implements SimpleSideBar.OnLetterTouchedChangeListener {


    private RecyclerView mFanceRecycle;
    private SimpleSideBar mFansSsb;
    ArrayList<FancBean> fancList = new ArrayList<>();
    private FancListAdapter mFancListAdapter;
    private TextView mFancTv;
    Handler mHandler = new Handler();
    private LinearLayoutManager mLinearLayoutManager;


    @Override
    public void initParams(Bundle bundel) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_fans_list;
    }

    @Override
    protected void initView(View contextView) {
        mFanceRecycle = contextView.findViewById(R.id.fanc_recycle);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mFanceRecycle.setLayoutManager(mLinearLayoutManager);
        mFansSsb = contextView.findViewById(R.id.fanc_ssb);
        mFancTv = contextView.findViewById(R.id.fanc_tv);
        mFancListAdapter = new FancListAdapter(R.layout.fanc_recycle_item, fancList);
        mFanceRecycle.setAdapter(mFancListAdapter);

    }

    @Override
    protected void setListener() {
        mFansSsb.setOnLetterTouchedChangeListener(this);
        mFanceRecycle.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int firstVisibleItemPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
                mFansSsb.setTouchIndex(fancList.get(firstVisibleItemPosition).getHeaderWord());
            }
        });
    }

    @Override
    protected void doBussiness() {
        fancList.add(new FancBean("拉拉"));
        fancList.add(new FancBean("伯伯"));
        fancList.add(new FancBean("爸爸"));
        fancList.add(new FancBean("阿姨"));
        fancList.add(new FancBean("老是"));
        fancList.add(new FancBean("老婆"));
        fancList.add(new FancBean("嘉佳"));
        fancList.add(new FancBean("王五"));
        fancList.add(new FancBean("赵六"));
        fancList.add(new FancBean("天启"));
        fancList.add(new FancBean("灰灰"));
        fancList.add(new FancBean("莎莎"));
        fancList.add(new FancBean("拉拉"));
        fancList.add(new FancBean("伯伯"));
        fancList.add(new FancBean("爸爸"));
        fancList.add(new FancBean("阿姨"));
        fancList.add(new FancBean("老是"));
        fancList.add(new FancBean("老婆"));
        fancList.add(new FancBean("嘉佳"));
        fancList.add(new FancBean("王五"));
        fancList.add(new FancBean("赵六"));
        fancList.add(new FancBean("天启"));
        fancList.add(new FancBean("灰灰"));
        fancList.add(new FancBean("莎莎"));
        Collections.sort(fancList, new Comparator<FancBean>() {
            @Override
            public int compare(FancBean lhs, FancBean rhs) {
                //根据拼音进行排序
                return lhs.getPingyin().compareTo(rhs.getPingyin());
            }
        });
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    public void onTouchedLetterChange(String letterTouched) {
        int letterPosition = mFancListAdapter.getLetterPosition(letterTouched);
        mFanceRecycle.scrollToPosition(letterPosition);
        mFancTv.setVisibility(View.VISIBLE);
        mFancTv.setText(letterTouched);
        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mFancTv.setVisibility(View.GONE);
            }
        }, 500);


    }
}
